package com.ym.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Logger {
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(Logger.class);
//	private static org.apache.log4j.Logger infoLog = org.apache.log4j.Logger.getLogger("appInfo");
//	private static org.apache.log4j.Logger debugLog = org.apache.log4j.Logger.getLogger("appDebug");
//	private static org.apache.log4j.Logger errorLog = org.apache.log4j.Logger.getLogger("appError");
//
//	public static void info(Object message) {
//		infoLog.info(message);
//	}
//
//	public static void debug(Object message) {
//		debugLog.debug(message);
//	}
//
//	public static void error(Object message) {
//		errorLog.error(message);
//	}
//

//
//	public static String exceptionToString(Throwable e){
//		StringWriter sw = new StringWriter();
//		PrintWriter pw = new PrintWriter(sw, true);
//		e.printStackTrace(pw);
//		pw.flush();
//		sw.flush();
//		return sw.toString();
//	}
	public static void info(Object message) {
		logger.info(StringHandler.getString(message));
	}

	public static void info(String message,Object... args) {
		logger.info(String.format(message,args));
	}

	public static void debug(Object message) {
		logger.debug(StringHandler.getString(message));
	}

	public static void debug(String message,Object... args) {
		logger.debug(String.format(message,args));
	}
	public static void error(Object object) {
		if(object instanceof Throwable){
			logger.error(exceptionToString((Throwable)object));
		}else {
			logger.error(JSON.toJSONString(object));
		}
	}

	public static void error(String message,Throwable e) {
		logger.error(String.format(message.contains("%s")?message:(message+"%s"),exceptionToString(e)));
	}
	public static String exceptionToString(Throwable e){
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw, true);
		e.printStackTrace(pw);
		pw.flush();
		sw.flush();
		return sw.toString();
	}
}
